{% extends 'layout.html' %}

{% block content %}
    <div class="container">
        <div>
            <input id="btnAdd" type="button" value="新建订单" class="btn btn-primary">
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                订单列表
            </div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>id</th>
                    <th>名称</th>
                    <th>订单号</th>
                    <th>价格</th>
                    <th>状态</th>
                    <th>负责人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        <th>{{ obj.id }}</th>
                        <td>{{ obj.title }}</td>
                        <td>{{ obj.oid }}</td>
                        <td>{{ obj.price }}</td>
                        <td>{{ obj.get_status_display }}</td>
                        <td>{{ obj.admin.username }}</td>
                        <td>
                            <input id="{{ obj.id }}" type="button" class="btn btn-primary btn-xs delEdit" value="编辑">
                            <input id="{{ obj.id }}" type="button" class="btn btn-danger btn-xs delBtn" value="删除">
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- 新增/编辑弹窗 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建</h4>
                </div>
                <div class="modal-body">
                    <form id="formAdd">
                        <div class="clearfix">
                            {% for foo in form %}
                                <div class="col-xs-6">
                                    <div class="form-group" style="position: relative; margin-bottom: 20px">
                                        <label>{{ foo.label }}</label>
                                        {{ foo }}
                                        <span class="error_msg" style="color: red; position: absolute"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button id="btnSave" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 删除弹窗 -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">

            <div class="alert alert-danger alert-dismissable fade in" role="alert">
                <h4>是否确定删除</h4>
                <p style="margin: 20px 0">删除后所有相关数据都会被删除</p>
                <p style="text-align: right">
                    <button type="button" class="btn btn-danger" id="btnConfirm">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </p>
            </div>

        </div>
    </div>
{% endblock %}

{% block js %}
    <script>
        var DELETEID;
        var EDITID;

        $(function () {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDelEvent();
            bindBtnConfirmEvent();
            bindBtnEditEvent();
        })

        function bindBtnAddEvent() {
            $('#btnAdd').click(function () {
                {#清空对话框中的数据#}
                EDITID = undefined
                $('#formAdd')[0].reset();
                $('#myModal').modal('show')
                $('#myModalLabel').text('新增')
            })
        }

        function bindBtnSaveEvent() {
            $('#btnSave').click(function () {

                $('.error_msg').empty()  //置空错误信息
                if (EDITID) {
                    do_edit();
                } else {
                    do_add();
                }


            })
        }

        function do_edit() {
            $.ajax({
                url: '/order/edit/' + '?id=' + EDITID,
                type: 'post',
                data: $('#formAdd').serialize(),
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        alert('编辑成功');
                        {#关闭对话框#}
                        $('#myModal').modal('hide');
                        {#清空表单，$('#formAdd')是jquery对象$('#formAdd')[0]是dom对象#}
                        $('#formAdd')[0].reset();
                        location.reload()
                    } else {
                        if (res.tips) {
                            alert(res.tips);
                        } else {
                            $.each(res.error, function (name, data) {
                                $('#id_' + name).next().text(data[0])
                            })
                        }
                    }
                }
            })
        }

        function do_add() {
            $.ajax({
                url: '/order/save/',
                type: 'post',
                data: $('#formAdd').serialize(),
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        alert('添加成功');
                        {#关闭对话框#}
                        $('#myModal').modal('hide');
                        {#清空表单，$('#formAdd')是jquery对象$('#formAdd')[0]是dom对象#}
                        $('#formAdd')[0].reset();
                        location.reload()
                    } else {
                        $.each(res.error, function (name, data) {
                            $('#id_' + name).next().text(data[0])
                        })
                    }
                }
            })
        }

        function bindBtnDelEvent() {
            $('.delBtn').click(function () {
                $('#deleteModal').modal('show')
                {#取到这一行中的属性id#}
                DELETEID = $(this).attr('id')
            })
        }

        function bindBtnConfirmEvent() {
            $('#btnConfirm').click(function () {
                $.ajax({
                    url: '/order/delete/',
                    type: 'GET',
                    data: {
                        id: DELETEID
                    },
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            $('#deleteModal').modal('hide');
                            location.reload()
                        } else {
                            alert(res.error)
                            $('#deleteModal').modal('hide');
                            location.reload()
                        }
                    }
                })
            })
        }

        function bindBtnEditEvent() {
            $('.delEdit').click(function () {
                {#获取这一条的id#}
                var currentId = $(this).attr('id')
                EDITID = currentId

                $.ajax({
                    url: '/order/detail/',
                    type: 'get',
                    data: {
                        id: currentId
                    },
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            console.log(res)
                            $.each(res.data, function (k, v) {
                                $('#id_' + k).val(v)
                            })
                            $('#myModalLabel').text('编辑')
                            $('#myModal').modal('show')
                        } else {
                            alert(res.error)
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}